<template>
  <div class="cms-upload" style="margin-top: 5px;margin-left: 10px">
    <el-upload
      class="upload-demo inline-block"
      name="uploadFile"
      :action="upLoadUrl"
      :on-change="handleChange"
      :limit="1"
      accept=".mp4"
      :data="data"
      :on-exceed="handleExceed"
      :on-success="handleAvatarSuccess"
      :on-progress="handleAvatarProgress"
      :file-list="fileList"
      :show-file-list="false"
      ref="upload">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip"></div>
    </el-upload>
    <div v-if="showFileList && uploading" class="w-350">
      <span class="file-gray">正在上传：</span>
      <span class="file-gray"><i class="el-icon-document"></i>{{fileName}}</span>
      <el-progress :percentage="percentage" :status="status"></el-progress>
    </div>
    <div v-if="showFileList && fileList.length>0">
      <span class="file-gray">文件列表：</span>
      <ul class="el-upload-list el-upload-list--text cms-file-items-box clearfix" style="width: 50%;">
        <li class="el-upload-list__item is-success cms-file-item" v-for="(item,index) in fileList" :key="index">
          <a class="el-upload-list__item-name cms-file-label" :title="item.name">
            <label>
              <i class="el-icon-document"></i>
              {{item.name}}
            </label>
            <i class="cms-delete-little iconfont icon-shanchu1" @click="delFileList(index)"></i>
          </a>
        </li>

      </ul>
    </div>
  </div>
</template>


<script>
  import {getRand} from "@/untils/random";
  import {signParams} from "@/untils/sign";
  import api from "@/api/api";

  let rand = getRand();

  export default {
    props: {
      field: {
        type: String,
        default: '',
      },
      videoId: {
        type: String,
        default: '',
      },
      uploadName: {
        type: String,
        default: "上传"
      },
      action: {
        type: String,
        default: ""
      },
      //是否支持多文件上传
      multiple: {
        type: false,
        default: true
      },
      //是否显示上传文件列表
      showFileList: {
        type: Boolean,
        default: true
      },
      //文件提交name值
      name: {
        type: String,
        default: "uploadFile"
      },
      //上传路径
      uploadPath: {
        type: String,
        default: ""
      },
    },
    data() {
      return {
        playAuth: "",
        upLoadUrl: this.$store.state.sys.baseUrl + api.attachmentBatchUpload,
        fileList: [],
        percentage: 0,
        status: '0',
        uploadStar: false,
        dialogVisible: false,
        data: {},
        params: {
          appId: process.env.appId,
          sessionKey: localStorage.getItem("sessionKey"),
          type: "video",
          nonceStr: rand,
          uploadPath: this.uploadPath
        },
        uploading: false,
        dialogLoading: false
      };
    },
    methods: {
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      handleChange() {
      },
      handleAvatarSuccess(res, file) {//上传成功服务器响应
        this.dialogLoading = true;
        let attachmentList = [];
        if (res.code == "200" && res.body != '') {
          this.status = 'success';
          attachmentList = res.body;
          for (let i in attachmentList) {
            let attachment = attachmentList[i];
            this.fileList.push({name: attachment.fileName, path: attachment.filePath});
          }
          this.dialogLoading = false;
        } else {
          this.errorMessage(res.code + ":" + res.message);
          this.status = "exception";
          this.errorMessage("上传失败:" + res.code);
        }
        setTimeout(() => {
          this.uploadStar = false;
          this.percentage = 0;
          this.status = "";
          this.videoId = "";
          this.$emit('change', attachmentList);
        }, 2000);
      },
      handleAvatarProgress(event, file, fileList) {//进度
        this.videoId = "";
        this.uploadStar = true;
        this.percentage = parseInt(event.percent.toFixed(1));
      },
      delFileList(index) {
        this.fileList.splice(index, 1);
        this.$refs.upload.clearFiles();
        this.$emit('remove', index);//返回列表
      }
    },
    created() {
      this.data = signParams(this.params, process.env.appKey);
    }
  }
</script>

<style>
  .inline-block {
    display: inline-block;
  }
</style>
